home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / TinyGL.lha / tinygl / src / zgl.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-25  |  8.6 KB  |  389 lines

  1. #ifndef _tgl_zgl_h_
  2. #define _tgl_zgl_h_
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <GL/gl.h>
  10. #include "zbuffer.h"
  11. #include "zmath.h"
  12. #include "zfeatures.h"
  13.                                         
  14. //#include <mpatrol.h>
  15.  
  16. #define DEBUG
  17. /* #define NDEBUG */
  18.  
  19. #ifdef __SASC
  20. #define inline
  21. #endif
  22.  
  23. #ifndef M_PI
  24. #define M_PI 3.141592
  25. #endif
  26.  
  27. enum {
  28.  
  29. #define ADD_OP(a,b,c) OP_ ## a ,
  30.  
  31. #include "opinfo.h"
  32.  
  33. };
  34.  
  35. /* initially # of allocated GLVertexes (will grow when necessary) */
  36. #define POLYGON_MAX_VERTEX 16
  37.  
  38. /* Max # of specular light pow buffers */
  39. #define MAX_SPECULAR_BUFFERS 8
  40. /* # of entries in specular buffer */
  41. #define SPECULAR_BUFFER_SIZE 1024
  42. /* specular buffer granularity */
  43. #define SPECULAR_BUFFER_RESOLUTION 1024
  44.  
  45.  
  46. #define MAX_MODELVIEW_STACK_DEPTH  32
  47. #define MAX_PROJECTION_STACK_DEPTH 8
  48. #define MAX_TEXTURE_STACK_DEPTH    8
  49. #define MAX_NAME_STACK_DEPTH       64
  50. #define MAX_TEXTURE_LEVELS         11
  51. #define MAX_LIGHTS                 16
  52.  
  53. #define VERTEX_HASH_SIZE 1031
  54.  
  55. #define MAX_DISPLAY_LISTS 1024
  56. #define OP_BUFFER_MAX_SIZE 512
  57.  
  58. #define TGL_OFFSET_FILL    0x1
  59. #define TGL_OFFSET_LINE    0x2
  60. #define TGL_OFFSET_POINT   0x4
  61.  
  62. typedef struct GLSpecBuf {
  63.   int shininess_i;
  64.   int last_used;
  65.   float buf[SPECULAR_BUFFER_SIZE+1];
  66.   struct GLSpecBuf *next;
  67. } GLSpecBuf;
  68.  
  69. typedef struct GLLight {
  70.   V4 ambient;
  71.   V4 diffuse;
  72.   V4 specular;
  73.   V4 position;
  74.   V3 spot_direction;
  75.   float spot_exponent;
  76.   float spot_cutoff;
  77.   float attenuation[3];
  78.   /* precomputed values */
  79.   float cos_spot_cutoff;
  80.   V3 norm_spot_direction;
  81.   V3 norm_position;
  82.   /* we use a linked list to know which are the enabled lights */
  83.   int enabled;
  84.   struct GLLight *next,*prev;
  85. } GLLight;
  86.  
  87. typedef struct GLMaterial {
  88.   V4 emission;
  89.   V4 ambient;
  90.   V4 diffuse;
  91.   V4 specular;
  92.   float shininess;
  93.  
  94.   /* computed values */
  95.   int shininess_i;
  96.   int do_specular;
  97. } GLMaterial;
  98.  
  99.  
  100. typedef struct GLViewport {
  101.   int xmin,ymin,xsize,ysize;
  102.   V3 scale;
  103.   V3 trans;
  104.   int updated;
  105. } GLViewport;
  106.  
  107. typedef union {
  108.   int op;
  109.   float f;
  110.   int i;
  111.   unsigned int ui;
  112.   void *p;
  113. } GLParam;
  114.  
  115. typedef struct GLParamBuffer {
  116.   GLParam ops[OP_BUFFER_MAX_SIZE];
  117.   struct GLParamBuffer *next;
  118. } GLParamBuffer;
  119.  
  120. typedef struct GLList {
  121.   GLParamBuffer *first_op_buffer;
  122.   /* TODO: extensions for an hash table or a better allocating scheme */
  123. } GLList;
  124.  
  125. typedef struct GLVertex {
  126.   int edge_flag;
  127.   V3 normal;
  128.   V4 coord;
  129.   V4 tex_coord;
  130.   V4 color;
  131.  
  132.   /* computed values */
  133.   V4 ec;                /* eye coordinates */
  134.   V4 pc;                /* coordinates in the normalized volume */
  135.   int clip_code;        /* clip code */
  136.   ZBufferPoint zp;      /* integer coordinates for the rasterization */
  137. } GLVertex;
  138.  
  139. typedef struct GLImage {
  140.   void *pixmap;
  141.   int xsize,ysize;
  142. } GLImage;
  143.  
  144. /* textures */
  145.  
  146. #define TEXTURE_HASH_TABLE_SIZE 256
  147.  
  148. typedef struct GLTexture {
  149.   GLImage images[MAX_TEXTURE_LEVELS];
  150.   int handle;
  151.   struct GLTexture *next,*prev;
  152. } GLTexture;
  153.  
  154.  
  155. /* shared state */
  156.  
  157. typedef struct GLSharedState {
  158.   GLList **lists;
  159.   GLTexture **texture_hash_table;
  160. } GLSharedState;
  161.  
  162. struct GLContext;
  163.  
  164. typedef void (*gl_draw_triangle_func)(struct GLContext *c,
  165.                                       GLVertex *p0,GLVertex *p1,GLVertex *p2);
  166.  
  167. /* display context */
  168.  
  169. typedef struct GLContext {
  170.   /* Z buffer */
  171.   ZBuffer *zb;
  172.  
  173.   /* lights */
  174.   GLLight lights[MAX_LIGHTS];
  175.   GLLight *first_light;
  176.   V4 ambient_light_model;
  177.   int local_light_model;
  178.   int lighting_enabled;
  179.   int light_model_two_side;
  180.  
  181.   /* materials */
  182.   GLMaterial materials[2];
  183.   int color_material_enabled;
  184.   int current_color_material_mode;
  185.   int current_color_material_type;
  186.  
  187.   /* textures */
  188.   GLTexture *current_texture;
  189.   int texture_2d_enabled;
  190.  
  191.   /* shared state */
  192.   GLSharedState shared_state;
  193.  
  194.   /* current list */
  195.   GLParamBuffer *current_op_buffer;
  196.   int current_op_buffer_index;
  197.   int exec_flag,compile_flag,print_flag;
  198.  
  199.   /* matrix */
  200.  
  201.   int matrix_mode;
  202.   M4 *matrix_stack[3];
  203.   M4 *matrix_stack_ptr[3];
  204.   int matrix_stack_depth_max[3];
  205.  
  206.   M4 matrix_model_view_inv;
  207.   M4 matrix_model_projection;
  208.   int matrix_model_projection_updated;
  209.   int matrix_model_projection_no_w_transform;
  210.   int apply_texture_matrix;
  211.  
  212.   /* viewport */
  213.   GLViewport viewport;
  214.  
  215.   /* current state */
  216.   int polygon_mode_back;
  217.   int polygon_mode_front;
  218.  
  219.   int current_front_face;
  220.   int current_shade_model;
  221.   int current_cull_face;
  222.   int cull_face_enabled;
  223.   int normalize_enabled;
  224.   gl_draw_triangle_func draw_triangle_front,draw_triangle_back;
  225.  
  226.   /* selection */
  227.   int render_mode;
  228.   unsigned int *select_buffer;
  229.   int select_size;
  230.   unsigned int *select_ptr,*select_hit;
  231.   int select_overflow;
  232.   int select_hits;
  233.  
  234.   /* names */
  235.   unsigned int name_stack[MAX_NAME_STACK_DEPTH];
  236.   int name_stack_size;
  237.  
  238.   /* clear */
  239.   float clear_depth;
  240.   V4 clear_color;
  241.  
  242.   /* current vertex state */
  243.   V4 current_color;
  244.   unsigned int longcurrent_color[3]; /* precomputed integer color */
  245.   V4 current_normal;
  246.   V4 current_tex_coord;
  247.   int current_edge_flag;
  248.  
  249.   /* glBegin / glEnd */
  250.   int in_begin;
  251.   int begin_type;
  252.   int vertex_n,vertex_cnt;
  253.   int vertex_max;
  254.   GLVertex *vertex;
  255.  
  256.   /* opengl 1.1 arrays  */
  257.   float *vertex_array;
  258.   int vertex_array_size;
  259.   int vertex_array_stride;
  260.   float *normal_array;
  261.   int normal_array_stride;
  262.   float *color_array;
  263.   int color_array_size;
  264.   int color_array_stride;
  265.   float *texcoord_array;
  266.   int texcoord_array_size;
  267.   int texcoord_array_stride;
  268.   int client_states;
  269.  
  270.   /* opengl 1.1 polygon offset */
  271.   float offset_factor;
  272.   float offset_units;
  273.   int offset_states;
  274.  
  275.   /* specular buffer. could probably be shared between contexts,
  276.     but that wouldn't be 100% thread safe */
  277.   GLSpecBuf *specbuf_first;
  278.   int specbuf_used_counter;
  279.   int specbuf_num_buffers;
  280.  
  281.   /* opaque structure for user's use */
  282.   void *opaque;
  283.   /* resize viewport function */
  284.   int (*gl_resize_viewport)(struct GLContext *c,int *xsize,int *ysize);
  285.  
  286.   /* depth test */
  287.   int depth_test;
  288. } GLContext;
  289.  
  290. extern GLContext *gl_ctx;
  291.  
  292. void gl_add_op(GLParam *p);
  293.  
  294. /* clip.c */
  295. void gl_transform_to_viewport(GLContext *c,GLVertex *v);
  296. void gl_draw_triangle(GLContext *c,GLVertex *p0,GLVertex *p1,GLVertex *p2);
  297. void gl_draw_line(GLContext *c,GLVertex *p0,GLVertex *p1);
  298. void gl_draw_point(GLContext *c,GLVertex *p0);
  299.  
  300. void gl_draw_triangle_point(GLContext *c,
  301.                             GLVertex *p0,GLVertex *p1,GLVertex *p2);
  302. void gl_draw_triangle_line(GLContext *c,
  303.                            GLVertex *p0,GLVertex *p1,GLVertex *p2);
  304. void gl_draw_triangle_fill(GLContext *c,
  305.                            GLVertex *p0,GLVertex *p1,GLVertex *p2);
  306. void gl_draw_triangle_select(GLContext *c,
  307.                              GLVertex *p0,GLVertex *p1,GLVertex *p2);
  308.  
  309. /* matrix.c */
  310. void gl_print_matrix(const float *m);
  311. /*
  312. void glopLoadIdentity(GLContext *c,GLParam *p);
  313. void glopTranslate(GLContext *c,GLParam *p);*/
  314.  
  315. /* light.c */
  316. void gl_add_select(GLContext *c,unsigned int zmin,unsigned int zmax);
  317. void gl_enable_disable_light(GLContext *c,int light,int v);
  318. void gl_shade_vertex(GLContext *c,GLVertex *v);
  319.  
  320. void glInitTextures(GLContext *c);
  321. void glEndTextures(GLContext *c);
  322. GLTexture *alloc_texture(GLContext *c,int h);
  323.  
  324. /* image_util.c */
  325. void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb,
  326.                              int xsize,int ysize);
  327. void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb,
  328.                                int xsize, int ysize);
  329. void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest,
  330.                     unsigned char *src,int xsize_src,int ysize_src);
  331. void gl_resizeImageNoInterpolate(unsigned char *dest,int xsize_dest,int ysize_dest,
  332.                                  unsigned char *src,int xsize_src,int ysize_src);
  333.  
  334. GLContext *gl_get_context(void);
  335.  
  336. void gl_fatal_error(char *format, ...);
  337.  
  338.  
  339. /* specular buffer "api" */
  340. GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i,
  341.                               const float shininess);
  342.  
  343. #ifdef __BEOS__
  344. void dprintf(const char *, ...);
  345.  
  346. #else /* !BEOS */
  347.  
  348. #ifdef __SASC
  349. void dprintf(const char *, ...);
  350. #else
  351.  
  352. #ifdef DEBUG
  353.  
  354. #define dprintf(format, args...)  \
  355.   fprintf(stderr,"In '%s': " format "\n",__FUNCTION__, ##args);
  356.  
  357. #else
  358.  
  359. #define dprintf(format, args...)
  360.  
  361. #endif /* DEBUG */
  362. #endif /* __SASC */
  363. #endif /* !BEOS */
  364.  
  365. /* glopXXX functions */
  366.  
  367. #define ADD_OP(a,b,c) void glop ## a (GLContext *,GLParam *);
  368. #include "opinfo.h"
  369.  
  370. /* this clip epsilon is needed to avoid some rounding errors after
  371.    several clipping stages */
  372.  
  373. #define CLIP_EPSILON (1E-5)
  374.  
  375. static inline int gl_clipcode(float x,float y,float z,float w1)
  376. {
  377.   float w;
  378.  
  379.   w=w1 * (1.0 + CLIP_EPSILON);
  380.   return (x<-w) |
  381.     ((x>w)<<1) |
  382.     ((y<-w)<<2) |
  383.     ((y>w)<<3) |
  384.     ((z<-w)<<4) |
  385.     ((z>w)<<5) ;
  386. }
  387.  
  388. #endif /* _tgl_zgl_h_ */
  389.